package com.ruoyi.walmart.service.address.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.PaymentMethod;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.walmart.State;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.walmart.mapper.address.StateMapper;
import com.ruoyi.walmart.service.address.StateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StateServiceImpl implements StateService {

    @Autowired
    StateMapper stateMapper;

    @Override
    @DataScope(StateAlias = "s")
    public List<State> selectByState(State state) {
        return stateMapper.selectByState(state);
    }

    @Override
    public List<TreeSelect> selectStateTreeList(State state) {
        List<State> stateList = SpringUtils.getAopProxy(this).selectByState(state);
        return buildStateTreeSelect(stateList);
    }

    @Override
    public List<State> buildStateTree(List<State> stateList) {
        List<State> returnList = new ArrayList<>();
        List<Long> collect = stateList.stream().map(State::getStateId).collect(Collectors.toList());
        for (State state:stateList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(state.getParentId())){
                recursionFn(stateList,state);
                returnList.add(state);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = stateList;
        }
        return returnList;
    }

    private void recursionFn(List<State> stateList , State state){
        // 得到子节点列表
        List<State> childList = getChildList(stateList,state);
        state.setChildrenStateForm(childList);
        for (State newState : childList)
        {
            if (hasChild(childList, newState))
            {
                recursionFn(childList, newState);
            }
        }
    }

    /**
     * 获得子节点
     * @param stateList
     * @param state
     * @return
     */
    private List<State> getChildList(List<State> stateList, State state){
        List<State> returnList = new ArrayList<>();
        Iterator<State> iterator = stateList.iterator();
        while (iterator.hasNext()){
            State n = (State) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == state.getStateId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<State> stateList, State state)
    {
        return getChildList(stateList, state).size() > 0;
    }

    @Override
    public List<TreeSelect> buildStateTreeSelect(List<State> stateList) {
        List<State> statesTree = buildStateTree(stateList);
        return statesTree.stream().map(state -> {
            TreeSelect treeSelect = new TreeSelect(state); // 用 State 构造 TreeSelect
            treeSelect.setValue(state.getStateCode());    // 将 TreeSelect 的 value 设置为 stateCode
            return treeSelect;
        }).collect(Collectors.toList());
    }
}
